package com.yc.pay.customer;

import com.rabbitmq.client.Channel;
import com.yc.pay.api.resp.YcPayNotifyData;
import com.yc.pay.config.MqConfig;
import com.yc.pay.config.YcPayConfig;
import com.yc.pay.model.PayNotice;
import com.yc.pay.service.IPayNoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 异步回调消费者
 */
@Component
public class PayNotifyCustomer {

    private static final Logger logger = LoggerFactory.getLogger(PayNotifyCustomer.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private YcPayConfig ycPayConfig;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IPayNoticeService payNoticeService;



    /**
     * 异步回调消费者
     * @param channel
     * @param message
     * @param ycPayNotifyData
     */
    @RabbitListener(queues = MqConfig.DEAD_LETTER_QUEUE_NAME)
    public void rec(Channel channel, Message message, YcPayNotifyData ycPayNotifyData){
        logger.info("########收到延时队列中的请求，请求参数：{}#######",ycPayNotifyData);

        // 判断是否需要再次通知
        PayNotice payNotice = payNoticeService.queryPayNotieByPaySerialNo(ycPayNotifyData.getPaySerialNo());
        if (5 >= payNotice.getNoticeCount() || "1".equals(payNotice.getStatus())){
            logger.info("流水号：{}通知达到5次，状态不正确",payNotice.getPaySerialNo());

            //手动确认
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                logger.error("IOException",e);
            }

            return;
        }

        //再次通知
        String rlt = restTemplate.postForObject(ycPayConfig.getMchNotifyUrl(),ycPayNotifyData,String.class);
        if ("success".equals(rlt)){
            //手动确认
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                logger.error("IOException",e);
            }

            payNoticeService.upatePayNotice(payNotice.getPaySerialNo(),"0");

            return;
        }

        Integer ttl = 300000;
        if(payNotice.getNoticeCount() == 1){
            ttl = 300000;
        }else if(payNotice.getNoticeCount() == 2){
            ttl = 300000;
        }else if(payNotice.getNoticeCount() == 3){
            ttl = 300000;
        }else if(payNotice.getNoticeCount() == 4){
            ttl = 300000;
        }else if(payNotice.getNoticeCount() == 5){
            ttl = 300000;
        }

        //继续放入队列 10m
        final MessagePostProcessor messagePostProcessor = new MyMessagePostProcessor(ttl);
        rabbitTemplate.convertAndSend(MqConfig.BUSINESS_EXCHANGE_NAME,"",ycPayNotifyData,messagePostProcessor);



    }
}
